﻿/********
* 
* 
*  Description:
*  Create By Kain at 4/6/2011 3:22:52 PM
*  
*
*  Revision History:
*  Date                  Who                 What
*  
* 
*/

#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

#endregion

namespace Cherry.Agile.ServiceModel.Runtime
{
    [Serializable]
    public class ServiceEventArgs:EventArgs
    {
        public ServiceInfo ServiceInfo { get; set; }

        public Exception Exception { get; set; }

        public string Type { get; set; }
    }

    [Serializable]
    public class EventListener:MarshalByRefObject,IEventListener
    {
        public event EventHandler<ServiceEventArgs> Notify;
        
        public void Started(ServiceInfo service)
        {
            System.Diagnostics.Trace.TraceInformation("EventListener\t" + service.FullName + " started:");
            foreach (var item in service.EndpointsInfo)
            {
                System.Diagnostics.Trace.TraceInformation("EventListener\t" + item.Address);
            }
            OnNotify(service, null, "started");
        }
        protected virtual void OnNotify(ServiceInfo service, Exception ex,string type) {
            if (Notify != null)
                Notify(this,new ServiceEventArgs() { ServiceInfo = service, Exception = ex, Type = type });
        }

        public void Info(ServiceInfo service, RunState state)
        {
             
        }

        public void Stoped(ServiceInfo service)
        {
            System.Diagnostics.Trace.TraceInformation("EventListener\t" + service.FullName + " stoped");
            OnNotify(service, null, "stoped");
        }

        public void Error(ServiceInfo service, Exception ex)
        {
            System.Diagnostics.Trace.TraceInformation("EventListener\t" + service.FullName + " error:");
            System.Diagnostics.Trace.TraceInformation("EventListener\t" + ex.ToString());
            OnNotify(service, ex, "error");
        }
    }

 
}
